package br.com.portalecbahia.customize;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
import br.com.portalecbahia.R;
import br.com.portalecbahia.model.Ranking;

public class RankingListAdapter extends BaseAdapter {
    
	private Context context;

    private List<Ranking> listRanking;
   
    public RankingListAdapter (Context context, List<Ranking> listRanking){
        this.context = context;
        this.listRanking = listRanking;
    }
    
	public int getCount() {
		// TODO Auto-generated method stub
		return  listRanking.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return listRanking.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
        // Get actual object
		Ranking ranking = listRanking.get(position);
		 // on View
        LayoutInflater inflater = (LayoutInflater)
            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.item_list_ranking , null);
        
        TextView positionTX = (TextView)view.findViewById(R.id.positionTX);
        TextView teamTX = (TextView)view.findViewById(R.id.teamTX);
        TextView pointsTX = (TextView)view.findViewById(R.id.pointsTX);
        TextView gamesTX = (TextView)view.findViewById(R.id.gamesTX);
        TextView winsTX = (TextView)view.findViewById(R.id.winsTX);
        TextView drawTX = (TextView)view.findViewById(R.id.drawTX);
        TextView losesTX = (TextView)view.findViewById(R.id.losesTX);
        TextView differenceGoalsTX = (TextView)view.findViewById(R.id.differenceGoalsTX);

    	RelativeLayout mainLL = (RelativeLayout)view.findViewById(R.id.mainRL);
        if(ranking.getChampionship()!=null ){
        	if((ranking.getChampionship().getId() == 2)  && (position<=1)){
        		mainLL.setBackgroundResource(R.color.gray_f);
        	}else if((ranking.getChampionship().getId() == 1) && (position <=3)){
        		mainLL.setBackgroundResource(R.color.gray_f);
        	}else if((ranking.getChampionship().getId() == 4) && (position <=1)){
        		mainLL.setBackgroundResource(R.color.gray_f);
        	}else if((ranking.getChampionship().getId() == 1) && (position >=16)){
        		mainLL.setBackgroundResource(R.color.red2);
        	}else if((ranking.getChampionship().getId() == 3) && (position >=10)){
        		mainLL.setBackgroundResource(R.color.red2);
        	}
        }
	       
	        if(ranking.getPosition()!=null){
	        	positionTX.setText(ranking.getPosition()+"");
	        }
	        	
	        
	        if(ranking.getTeam()!=null){
	        	teamTX.setText(ranking.getTeam());
	        }
	        
	        if(ranking.getPoints()!=null){
	        	if(ranking.getPoints()<10)
	        		pointsTX.setText("0"+ranking.getPoints()+"");
	        	else
	        		pointsTX.setText(ranking.getPoints()+"");
	        }else{
	        	pointsTX.setText("");
	        }
	        
	        if(ranking.getGames()!=null){
	        	if(ranking.getGames()<10)
	        		gamesTX.setText("0"+ranking.getGames()+"");
	        	else
	        		gamesTX.setText(ranking.getGames()+"");
	        }else{
	        	gamesTX.setText("");
	        }
	        
	        if(ranking.getWins()!=null){
	        	if(ranking.getWins()<10)
	        		winsTX.setText("0"+ranking.getWins()+"");
	        	else
	        		winsTX.setText(ranking.getWins()+"");
	        }else{
	        	winsTX.setText("");
	        }
	        
	        if(ranking.getDraws()!=null){
	        	if(ranking.getDraws()<10)
	        		drawTX.setText("0"+ranking.getDraws()+"");
	        	else
	        		drawTX.setText(ranking.getDraws()+"");
	        }else{
	        	drawTX.setText("");
	        }
	        
	        if(ranking.getLosses()!=null){
	        	if(ranking.getLosses()<10)
	        		losesTX.setText("0"+ranking.getLosses()+"");
	        	else
	        		losesTX.setText(ranking.getLosses()+"");
	        }else{
	        	losesTX.setText("");
	        }
	        
	        if(ranking.getGoalDifference()!=null){
	        	if((ranking.getGoalDifference()<10 && ranking.getGoalDifference()>0))
	        		differenceGoalsTX.setText("0"+ranking.getGoalDifference()+"");
	        	else
	        		differenceGoalsTX.setText(ranking.getGoalDifference()+"");
	        }else{
	        	differenceGoalsTX.setText("");
	        }
      
        	
        

        
        
        
        return view;
	}

	
	 
	
	

}
